function getContainerHtml(){
	var containerStart = "<div class = \"container\" id = \"restIntro\">";
	var containerEnd = "</div>";
	return containerStart+containerEnd;
}

function getRestaurantDes(data){
	var name,address,argC,cuisines,contactNum,desc;

	name = data["name"];
	address = data["address"];
	argC = data["argC"];
	cuisines = data["cuisines"];
	contactNum = data["contactNum"];
	desc = data["description"];
	var containerStart = "<div class = \"resultsContainer\"> <div class = \"row\">";
	var containerEnd = "</div>";
	// alert("Before "+address.length);

	var addressBtn = address.split(' ').join('_');

	// alert("After "+addressBtn.length);

	var restDesc = getRestDesc(name,address,argC,cuisines,contactNum,desc);
	var imgDesc = getImageHtml(name);
	var bookButtonDiv = getBookBtn(addressBtn);
	var datePicker = getDatePicker(addressBtn);
	var restaurantDescription = restDesc+imgDesc;
	return containerStart + restaurantDescription + containerEnd + bookButtonDiv +datePicker+ containerEnd+"<br />";
}

function getDescriptionHtml(data){ 
	var id = data["name"];
	var rowClass = "row";

	var divStart = "<div class = \""+rowClass+"\" id=\""+id+"\">";
	var btn = "<button class = \"btn btn-primary\">Book</button>";
	var divEnd = "</div>";
	var restDescription = getRestaurantDes(data);
	return divStart+restDescription+divEnd;
}

$(function(){
		$("#searchBtn").click(function(events){
			events.preventDefault();
			var res_name = $("#searchName").val();
			var res_cuisine_index = $("#searchCuisines").val();
			var res_aveC_index = $("#avgcom").val();
			var max_price, min_price;

			switch(res_aveC_index){
				case "0": max_price = ""; min_price = ""; break;
				case "1": max_price = "30"; min_price = "0"; break;
				case "2": max_price = "50"; min_price = "30"; break;
				case "3": max_price = "100"; min_price = "50"; break;
				case "4": max_price = ""; min_price = "100"; break;
				default: break; 
			}
			
			setCookie("searchName",res_name,365);
			setCookie("searchCuisines",res_cuisine_index,365);
			setCookie("searchMaxPrice",max_price,365);
			setCookie("searchMinPrice",min_price,365);


			var rankCriteriaIndex = 0;

			resetHtml(res_name,res_cuisine_index,max_price,min_price,rankCriteriaIndex);
  	});
});

function resetHtml(res_name,res_cuisine_index,max_price,min_price,rankCriteriaIndex){
		var resDescriptionHtml;
		var containerHtml = getContainerHtml();
		var container = $("#restIntroduction").html(containerHtml);

  		$.post("http://localhost/cs2102/src/search.php", 
  		{
  			name: res_name,
  			cuisines: res_cuisine_index,
  			maxPrice: max_price,
  			minPrice: min_price,
  			rankIndex: rankCriteriaIndex
  		},

  		function(dataJson,status){
  			var dat = $.parseJSON(dataJson);
  			var resDescriptionHtml;
  			var data = dat.res0;
  			var i = 0;

  			date = dat["res0"];
  			var searchButton  = getSearchButton();
  			$("#restIntro").append(searchButton);

  			while(data != undefined){
  				// alert(data.name);
  				resDescriptionHtml = getRestaurantDes(data);
  				$("#restIntro").append(resDescriptionHtml);
  				i++;
  				data = dat["res"+i];
  			}

		  	$(".restTitle").click(function(){
				var address = this.id;
				setCookie("restAddress",address,365);
			});
  		});
}

function getSearchButton(){
	var sortBtn = "<div class=\"row\">";
	sortBtn += "<input type=\"submit\" id = \"sortBtn_1\" class=\"btn btn-primary\" value = \"Sort By Name\" onclick=\"sortByName()\"/>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp";
	sortBtn += "<input type=\"submit\" id = \"sortBtn_2\" class=\"btn btn-primary\" value = \"Sort By Price\" onclick=\"sortByPrice()\"/>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp";
	sortBtn += "<input type=\"submit\" id = \"sortBtn_2\" class=\"btn btn-primary\" value = \"Sort By Cuisines\" onclick=\"sortByCuisines()\"/> <hr />";
	sortBtn += "</div>";
	return sortBtn;
}

function getImageHtml(name){
	var divStartImg = "<div class = \"col-md-3\">";
	var imgPath = "../img/"+name+"/01.jpg";
	var image = "<img src = \""+imgPath+"\" height = \"150\" width = \"150\" id = \"img_01\" />";
	var divEndImg = "</div>";

	return divStartImg+image+divEndImg;
}

function getBookBtn(address){
	return "<div class = \"bookResults\"> <button class = \"btn btn-primary pull-right book-button\" value=\""+address+"\" id=\""+address+"\" onclick=\"selectDate(this)\">Book</button></div>";
	// return "<input type=\"button\" onclick=\"selectDate()\" name=\"Book\" value=\"Book\" class = \"btn btn-primary pull-right book-button\"/>";
}

function getDatePicker(address){
	return "<div id=\"datePicker_"+address+"\"></div>";
}

function getRestDesc(name,address,argC,cuisines,contactNum,desc){
	var divStart = "<div class = \"col-md-9\">";
	var header = "<a href=\"restaurants.html\"><h3 id = \""+address+"\" value = \""+name+"\" class = \"restTitle\">"+name+"</h3></a>";
	var descBody = "<p class = \"displayDesc\">"+desc+"</p>";
	descBody += "<br><p class = \"displayCuisines\"><strong>Cuisine:</strong>: "+cuisines+"</p>";
	descBody += "<br><p class = \"displayAddr\"><strong>Address</strong>: "+address+"</p>";
	descBody += "<br><p class = \"displayAvgC\"><strong>Average Consumption</strong>: "+argC+"</p>";
	descBody += "<br><p class = \"displayMobile\"><strong>Contact</strong>: "+contactNum+"</p>";
	var divEnd = "</div>";

	setCookie("rName"+address,name,365);
	return divStart+header+divEnd+divStart+descBody+divEnd;
}